package com.xxx.springsecuritydemo.handler;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

// 登录成功处理器
public class MyAuthenticationSuccessHandler implements AuthenticationSuccessHandler {

    // 自定义url
    private String url;

    // 自定义实现构造方法
    public MyAuthenticationSuccessHandler(String url) {
        this.url = url;
    }

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        // 获取请求ip地址，然后输出
        System.out.println(request.getRemoteAddr());
        // 测试一下过滤器认证中的principal是不是user对象
        User user = (User) authentication.getPrincipal();
        System.out.println(user.getUsername());
        // 不会直接输出密码，直接打印null
        System.out.println(user.getPassword());
        System.out.println(user.getAuthorities());
        response.sendRedirect(url);
    }
}
